package dao

import (
	"myserver/model"
	"myserver/param"
	"myserver/tool"
	"strconv"
)

type FamilyDao struct {
	*tool.MyOrm
}

func (d *FamilyDao) GetFamily(id string) model.Family {
	var family = model.Family{}
	familyId, err := strconv.ParseUint(id, 10, 64)
	if err != nil {
		return family
	}
	family.ID = uint(familyId)
	first := d.Db.First(&family)
	if first.RowsAffected == 0 {
		family.ID = 0
		return family
	}
	return family
}

func (d *FamilyDao) CreateFamily(family model.Family) int64 {
	res := d.Db.Create(&family)
	return res.RowsAffected
}

func (d *FamilyDao) DeleteFamily(family model.Family) int64 {
	res := d.Db.Delete(&family)
	return res.RowsAffected
}

func (d *FamilyDao) UpdateFamily(family model.Family) int64 {
	res := d.Db.Updates(&family)
	return res.RowsAffected
}

func (d *FamilyDao) GetFamilyPage(pageParam param.PageParam) param.ResponsePageParam {
	rs := param.ResponsePageParam{}
	rs.Data = []model.Family{}
	db := d.Db.Model(&model.Family{})
	if pageParam.SearchContent != "" {
		db = db.Where("house_owner LIKE ?", "%"+pageParam.SearchContent+"%")
	}
	db.Order("ID ASC").Count(&rs.Total).Limit(pageParam.Limit).Offset(pageParam.Page).Find(&rs.Data)
	return rs
}
